﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace UI.Admin
{
    public partial class tintuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }
        void HienThiDanhSach()
        {
            // Get All Sản Phẩm
            DataTable Manufacturers = DBConnection.getALL("sp_getAllTinTuc");
            GridView1.DataSource = Manufacturers;
            GridView1.DataBind();

            MultiView1.ActiveViewIndex = 0;
            btninsert.Enabled = false;
        }


        void HienThiKetQuaSua(String manxb)
        {

            SqlParameter[] thamso = new SqlParameter[]
            {
            new SqlParameter("@MaTT",manxb)
            };
            DataTable dtsp = DBConnection.GetDatatableByStoreProcedureName("sp_TinTucbyMaTT", thamso);
            lbmatt.Text = dtsp.Rows[0]["MaTT"].ToString();
            txttieude.Text = dtsp.Rows[0]["TieuDe"].ToString();
            txtnoidung.Text = dtsp.Rows[0]["NoiDung"].ToString();
            txttomluoc.Text = dtsp.Rows[0]["TomLuoc"].ToString();
            txtngaytao.Text = dtsp.Rows[0]["NgayTao"].ToString();
            txttacgia.Text = dtsp.Rows[0]["TacGia"].ToString();
            if (dtsp.Rows[0]["HienThi"].ToString() == "True")
            {
                chkhienthi.Checked = true;
            }
            else
                chkhienthi.Checked = false;
            if (dtsp.Rows[0]["TinNoiBat"].ToString() == "True")
            {
                chktinnoibat.Checked = true;
            }
            else
                chktinnoibat.Checked = false;

            Image2.ImageUrl = "~/images/" + dtsp.Rows[0]["LinkImage"].ToString();



            MultiView1.ActiveViewIndex = 1;
        }


        protected void btntaomoi_Click(object sender, EventArgs e)
        {

            btnupdate.Visible = false;
            btninsert.Visible = true;
            MultiView1.ActiveViewIndex = 1;
            btninsert.Enabled = true;
            lbmatt.Text = "Mã tự động tăng";

        }


        protected void btntmkiem_Click(object sender, EventArgs e)
        {

            //string tennxb = txttim.Text;
            //SqlParameter[] thamso = new SqlParameter[]{
            //new SqlParameter("p_tennhaxb",tennxb)
            //};

            //// search NhaXB by TenNhaXB
            //DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchNhaXuatBanByTenNhaXB", thamso);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //txttim.Text = "";
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.HienThiDanhSach();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;

            string masp = GridView1.SelectedRow.Cells[2].Text;
            this.HienThiKetQuaSua(masp);
        }
        protected void btnback_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }


        protected void btnupdate_Click(object sender, EventArgs e)
        {

            SqlParameter[] thamso = new SqlParameter[] {
            new SqlParameter("@MaTT", int.Parse(lbmatt.Text)),
            };

            bool hienthi, noidung;
            if (chkhienthi.Checked == true)
            {
                hienthi = true;

            }
            else
                hienthi = false;
            if (chktinnoibat.Checked == true)
            {
                noidung = true;
            }
            else
                noidung = false;

            // get sampham by MaSP
            DataTable dt = DBConnection.GetDatatableByStoreProcedureName("sp_TinTucbyMaTT", thamso);
            string hinh;
            if (FileUpload2.HasFile)
            {
                string strfile = "~/images/" + FileUpload2.FileName;
                string s = Server.MapPath(strfile);
                FileUpload2.SaveAs(s);
                hinh = FileUpload2.FileName;
            }
            else
                hinh = dt.Rows[0]["LinkImage"].ToString();
            SqlParameter[] thamso1 = new SqlParameter[]
            {
            new SqlParameter("@MaTT",int.Parse(lbmatt.Text)),
            new SqlParameter("@TieuDe",txttieude.Text),
            new SqlParameter("@NoiDung",txtnoidung.Text),
            new SqlParameter("@TomLuoc",txttomluoc.Text),
            new SqlParameter("@NgayTao",DateTime.Parse(txtngaytao.Text)),
            new SqlParameter("@LinkImage",hinh),
            new SqlParameter("@TacGia",txttacgia.Text),
            new SqlParameter("@HienThi",hienthi),
            new SqlParameter("@TinNoiBat",noidung),
             };

            DBConnection.QueryByStoreProcedureName("sp_updateTinTucbyMaTT", thamso1);
            this.HienThiDanhSach();
        }


        protected void btninsert_Click(object sender, EventArgs e)
        {
            bool hienthi, noidung;
            if (chkhienthi.Checked == true)
            {
                hienthi = true;

            }
            else
                hienthi = false;
            if (chktinnoibat.Checked == true)
            {
                noidung = true;
            }
            else
                noidung = false;

            //tao ma nha xuat ban tu dong tang = lay max manhaxb+1 
            DataTable dt = DBConnection.getALL("countTinTuc");
            int maSP = int.Parse(dt.Rows[0]["soluong"].ToString()) + 2;

            string hinh = FileUpload2.FileName;
            SqlParameter[] thamso = new SqlParameter[]
            {
                new SqlParameter("@MaTT",maSP),
                new SqlParameter("@TieuDe",txttieude.Text),
                new SqlParameter("@NoiDung",txtnoidung.Text),
                new SqlParameter("@TomLuoc",txttomluoc.Text),
                new SqlParameter("@NgayTao",DateTime.Parse(txtngaytao.Text)),
                new SqlParameter("@LinkImage",hinh),
                new SqlParameter("@TacGia",txttacgia.Text),
                new SqlParameter("@HienThi",hienthi),
                new SqlParameter("@TinNoiBat",noidung),
             };
            DBConnection.QueryByStoreProcedureName("sp_insertTinTuc", thamso);

            this.HienThiDanhSach();
        }


        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string maSP = GridView1.Rows[e.RowIndex].Cells[2].Text;

            try
            {
                SqlParameter[] thamso = new SqlParameter[]
                 {
                new SqlParameter("@MaTT",int.Parse(maSP)),
                 };

                DBConnection.QueryByStoreProcedureName("deleteTinTuc", thamso);
            }
            catch
            {
                System.Console.WriteLine("khong xoa duoc do rang buoc khoa");
            }
            HienThiDanhSach();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
}